package com.cavalier.surtr.amop;

import com.cavalier.surtr.amop.provider.MessageSender;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.internet.MimeMessage;

/**
 * Created by archangle on 17/3/7.
 */
@Component
public final class JavaMailUtils {

    private static Logger logger = LoggerFactory.getLogger(MessageSender.class);

    @Autowired
    private JavaMailSenderImpl senderImpl;

    public void sendMailSystemLogger(String date, String subject, String domain, String requestURL, String message) throws Exception{
        MimeMessage mailMessage = this.senderImpl.createMimeMessage();
        senderImpl.setHost("smtp.qq.com");
        MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, true);
        messageHelper.setTo("1463067287@qq.com");
        messageHelper.setFrom("1539328607@qq.com");
        messageHelper.setSubject(date + " 系统异常");
        String msg = "<p>异常时间：" + date + "</p><p>门店企业：" + subject + "</p>"
                + "<p>部署环境：" + domain + "</p><p>异常连接：" + requestURL + "</p>"
                + "<p>异常内容：</p>" + message;
        messageHelper.setText("<html><head></head><body>" + msg + "</body></html>", true);
        senderImpl.send(mailMessage);
        logger.info("jmail push message success");
    }
}
